package com.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URLDecoder;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.framework.httpclient.builder.HCB;

public class HttpSend{
	
	static public void get(String url,String contentType) throws Exception{
		HttpGet httpPost = new HttpGet(url);
		
        CloseableHttpClient httpClient = HttpClients.createDefault();
        httpPost.addHeader("content-type", contentType);
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

        System.out.println("POST Response Status:: "
                + httpResponse.getStatusLine().getStatusCode());

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpResponse.getEntity().getContent()));

        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = reader.readLine()) != null) {
            response.append(inputLine);
        }
        reader.close();

        // print result
        
        System.out.println(response.toString());
	}
	
	
	static public String post(String url,String contentType,String params) throws Exception{
		HCB hcb = HCB.custom();
		//System.out.println(params);
		HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient httpClient = hcb.build();
        httpPost.addHeader("content-type", contentType);
        HttpEntity postParams = new StringEntity(params,"UTF-8");
        httpPost.setEntity(postParams);
        Long a = System.currentTimeMillis();
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
       // System.out.println(params);

       // System.out.println("POST Response Status:: "
           //     + httpResponse.getStatusLine().getStatusCode());

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpResponse.getEntity().getContent()));

        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = reader.readLine()) != null) {
            response.append(inputLine);
        }
        reader.close();

        // print result
        //System.out.println(System.currentTimeMillis()-a );
        //System.out.println(URLDecoder.decode(response.toString(),"UTF-8"));
        return URLDecoder.decode(response.toString(),"UTF-8");
	}
	
	static public String httpspost(String url,String contentType,String params) throws Exception{
		HCB hcb = HCB.custom();

		//System.out.println(url);
		//System.out.println(params);
		HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient httpClient = hcb.ssl().build();
        httpPost.addHeader("content-type", contentType);
        HttpEntity postParams = new StringEntity(params,"UTF-8");
        httpPost.setEntity(postParams);
        Long a = System.currentTimeMillis();
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        //System.out.println(params);

        //System.out.println("POST Response Status:: "
         //       + httpResponse.getStatusLine().getStatusCode());

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpResponse.getEntity().getContent()));

        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = reader.readLine()) != null) {
            response.append(inputLine);
        }
        reader.close();

        // print result
        //System.out.println(System.currentTimeMillis()-a );
        //System.out.println(URLDecoder.decode(response.toString(),"UTF-8"));
        return URLDecoder.decode(response.toString(),"UTF-8");
	}
	
	static public void main(String[] args) throws Exception{
		HCB hcb = HCB.custom();
		String url = "http://localhost:8080/cas/v1/tickets?username=a&password=1";
		hcb.timeout(6000);
		HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient httpClient = hcb.build();
        httpPost.addHeader("content-type", "application/x-www-form-urlencoded");
        
        Long a = System.currentTimeMillis();
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

        System.out.println("POST Response Status:: "
                + httpResponse.getStatusLine().getStatusCode());

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpResponse.getEntity().getContent()));

        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = reader.readLine()) != null) {
            response.append(inputLine);
        }
        reader.close();

        // print result
        System.out.println(System.currentTimeMillis()-a );
        System.out.println(URLDecoder.decode(response.toString(),"UTF-8"));
       // return URLDecoder.decode(response.toString(),"UTF-8");
	}
}